package com.ggg.clothesshop.service.IMPL;

import com.ggg.clothesshop.mapper.ProductMapper;
import com.ggg.clothesshop.model.Product;
import com.ggg.clothesshop.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceIMPL implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo<Product> selectByPage(int pageNum, int pageSize, String attributeValue) {

        List<String> array = new ArrayList<String>();
        String attribute = "%%";
        if(!"xxx".equals(attributeValue)){
            String av[] = attributeValue.split(",");
            attribute = "%,"+av[0]+",%";
            for(int i=1; i<av.length; i++){
                array.add( "%," + av[i] + ",%");
            }
        }

        PageHelper.startPage(pageNum, pageSize);
        ArrayList<Product> products = productMapper.selectProducts(attribute,array);
        PageInfo result = new PageInfo(products);
        return result;
    }

    @Override
    public Product selectById(String productId) {
        int id = 0;
        try {
            id = Integer.parseInt(productId);
        }catch (Exception e){
            return null;
        }
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public ArrayList<Product> selectAll() {
        String attribute = "%%";
        List<String> array = new ArrayList<String>();
        return productMapper.selectProducts(attribute,array);
    }
}
